package com.frank.im.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.frank.im.base.ImChatManage;
import com.frank.im.bean.MsgCont;
import com.frank.im.bean.MsgContExp;
import com.frank.im.bean.SessionRecord;
import com.frank.im.bean.SysMsgCont;
import com.frank.im.bean.UserTemp;

/**
 * 数据库帮助类
 * 
 * @author Frank
 * @version 1.0 Create by 2016.4.11
 */
public class DBHepler extends SQLiteOpenHelper {
	private static final String DB_NAME = "_sbc_im.db";
	private static final int VERSION = 1;
	private static DBHepler dbHepler = null;
	private static String curDbName = "";
	private static final String SQL_CREATE_SESSION_RECORD = "create table "
			+ SessionRecord.TABLENMAE + "(" + SessionRecord.ID
			+ " integer primary key autoincrement," + SessionRecord.USER_ID
			+ " integer," + SessionRecord.NO_READ_NUM + " integer,"
			+ SessionRecord.NEW_CONT + " text," + SessionRecord.NEW_CONT_TIME
			+ " text)";// 建立会话记录表语句

	private static final String SQL_CREATE_MSG_CONT = "create table "
			+ MsgCont.TABLENMAE + "(" + MsgCont.ID
			+ " integer primary key autoincrement," + MsgCont.REQUEST_ID
			+ " text," + MsgCont.SERVICE_CONT_ID + " integer,"
			+ MsgCont.FROM_ID + " integer," + MsgCont.TO_ID + " integer,"
			+ MsgCont.SEND_STATUS + " integer," + MsgCont.SEND_TIME + " text,"
			+ MsgCont.IS_SHOW_TIME + " integer," + MsgCont.MSG_CONT_TYPE
			+ " integer," + MsgCont.READ_STATUS + " integer)";// 建立消息内容表

	private static final String SQL_CREATE_MSG_CONT_EXP = "create table "
			+ MsgContExp.TABLENMAE + "(" + MsgContExp.ID
			+ " integer primary key autoincrement," + MsgContExp.MSG_CONT_ID
			+ " integer," + MsgCont.FROM_ID + " integer," + MsgCont.TO_ID
			+ " integer," + MsgContExp.EXP_TYPE + " integer," + MsgContExp.CONT
			+ " text)";// 建立消息内容拓展表

	private static final String SQL_CREATE_USER_TEMP = "create table "
			+ UserTemp.TABLENMAE + "(" + UserTemp.ID
			+ " integer primary key autoincrement," + UserTemp.USER_ID
			+ " integer," + UserTemp.USER_NAME + " text," + UserTemp.HEAD_IMG
			+ " text)";// 建立临时用户表

	private static final String SQL_CREATE_SYS_MSG_CONT = "create table "
			+ SysMsgCont.TABLENMAE + "(" + SysMsgCont.ID
			+ " integer primary key autoincrement,"
			+ SysMsgCont.SERVICE_CONT_ID + " integer," + SysMsgCont.READ_STATUS
			+ " integer," + SysMsgCont.SEND_TIME + " text," + SysMsgCont.CONT
			+ " text)";// 建立系统消息表

	private static final String SQL_DROP_SESSION_RECORD = "drop table if exists "
			+ SessionRecord.TABLENMAE;// 删除会话记录表语句

	private static final String SQL_DROP_MSG_CONT = "drop table if exists "
			+ MsgCont.TABLENMAE;// 删除消息内容表

	private static final String SQL_DROP_MSG_CONT_EXP = "drop table if exists "
			+ MsgContExp.TABLENMAE;// 删除消息内容拓展表

	private static final String SQL_DROP_USER_TEMP = "drop table if exists "
			+ UserTemp.TABLENMAE;// 删除临时用户表

	private static final String SQL_DROP_SYS_MSG_CONT = "drop table if exists "
			+ SysMsgCont.TABLENMAE;// 删除系统消息表

	private DBHepler(Context context) {
		super(context, getCurDb(), null, VERSION);
	}

	public static synchronized DBHepler getInstance(Context context) {
		if(!curDbName.equals("")&&!ImChatManage.getInstance().getAppMarkUtil()
				.getUserId().equals(curDbName)){
			dbHepler = null;
		}
		if (dbHepler == null) {
			dbHepler = new DBHepler(context);
		}
		return dbHepler;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SQL_CREATE_USER_TEMP);// 建立临时用户表
		db.execSQL(SQL_CREATE_SESSION_RECORD);// 建立会话记录表
		db.execSQL(SQL_CREATE_MSG_CONT);// 建立消息内容表
		db.execSQL(SQL_CREATE_MSG_CONT_EXP);// 建立消息内容表
		db.execSQL(SQL_CREATE_SYS_MSG_CONT);// 建立消息内容表
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		/*** drop ***/
		db.execSQL(SQL_DROP_USER_TEMP);// 删除临时用户表
		db.execSQL(SQL_DROP_SESSION_RECORD);// 删除会话记录表
		db.execSQL(SQL_DROP_MSG_CONT);// 删除会话记录表
		db.execSQL(SQL_DROP_MSG_CONT_EXP);// 删除会话记录表
		db.execSQL(SQL_DROP_SYS_MSG_CONT);// 删除会话记录表

		/*** create ***/
		db.execSQL(SQL_CREATE_USER_TEMP);// 建立临时用户表
		db.execSQL(SQL_CREATE_SESSION_RECORD);// 建立会话记录表
		db.execSQL(SQL_CREATE_MSG_CONT);// 建立消息内容表
		db.execSQL(SQL_CREATE_MSG_CONT_EXP);// 建立消息内容表
		db.execSQL(SQL_CREATE_SYS_MSG_CONT);// 建立消息内容表
	}

	public synchronized void closeDB() {
		if (dbHepler != null) {
			try {
				SQLiteDatabase db = dbHepler.getWritableDatabase();
				db.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			dbHepler = null;
		}
	}

	/**
	 * 获得当前数据库
	 * @return
	 */
	private static String getCurDb() {
		curDbName = ImChatManage.getInstance().getAppMarkUtil()
				.getUserId()
				+ DB_NAME;
		return curDbName;
	}

}
